//
//  MovieCollectionViewController.m
//  UI豆瓣2
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieCollectionViewController.h"
#import "Movie.h"
#import "MovieCollectionCell.h"
#import "MovieDetailViewController.h"
#import "DataBaseMovie.h"
@interface MovieCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *movieArr;
@property (nonatomic, retain)UICollectionView *collectionView;

@end

@implementation MovieCollectionViewController

- (void)dealloc
{
    [_movieArr release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";
    self.movieArr = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(95, 170);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flowLayout];
    
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[MovieCollectionCell class] forCellWithReuseIdentifier:@"item"];
    //设置底部区域大小
    flowLayout.footerReferenceSize = CGSizeMake(375, 110);
    [[DataBaseMovie shareDataBase] openDB];
    [[DataBaseMovie shareDataBase] createTable];
    self.movieArr = [[DataBaseMovie shareDataBase] selectMovie];
    
    [self getData];
}
- (void)getData{
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    
    [AppTools getDataWithPath:urlStr block:^(id result) {
        NSMutableDictionary *dic = result;
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        if ([reason isEqualToString:@"success"]) {
            NSLog(@"九宫格成功");
            for (NSDictionary *dic in array) {
                Movie *movie = [[Movie alloc] init];
                [movie setValuesForKeysWithDictionary:dic];
                [self.movieArr addObject:movie];
                [movie release];
                
            }
            [self.collectionView reloadData];
        }else {
            NSLog(@"请求失败");
        }
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.movieArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *itemID = @"item";
    MovieCollectionCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:itemID forIndexPath:indexPath];
    
    Movie *movie = [self.movieArr objectAtIndex:indexPath.item];
    item.movie = movie;
    return item;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailViewController *movieDetilVC = [[MovieDetailViewController alloc] init];
    Movie *movie = [self.movieArr objectAtIndex:indexPath.row];
    
    movieDetilVC.movieID = movie.movieId;
    [self.navigationController pushViewController:movieDetilVC animated:YES];
    [movieDetilVC release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
